package com.ar4j.pagination;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * Defines a pagination and sorting definition for retrieving records from the backing store.
 */
public class PaginationAndSort {
  public static final PaginationAndSort NONE = new PaginationAndSort();
  
  private int startRow;
  private int maxRows;
  private List<ISortField> sortFields;
  
  /**
   * Restrict to retrieving a page starting with startRow and containing maxRows rows.
   */
  public PaginationAndSort(int startRow, int maxRows) {
    this.startRow = startRow;
    this.maxRows = maxRows;
    this.sortFields = new ArrayList<ISortField>();
  }
  
  /**
   * Restrict to retrieving a page starting with startRow and containing maxRows rows and sorted 
   * by the given list of sort fields.
   */
  public PaginationAndSort(int startRow, int maxRows, ISortField ... fields) {
    this(startRow, maxRows);
    
    for(ISortField field : fields) {
      sortFields.add(field);
    }
  }
  
  /**
   * Retrieve data sorted by the given list of sort fields
   */
  public PaginationAndSort(ISortField ... fields) {
    this(0, Integer.MAX_VALUE, fields);
  }

  public int getStartRow() {
    return startRow;
  }

  public void setStartRow(int startRow) {
    this.startRow = startRow;
  }

  public int getMaxRows() {
    return maxRows;
  }

  public void setMaxRows(int maxRows) {
    this.maxRows = maxRows;
  }

  public List<ISortField> getSortFields() {
    return sortFields;
  }

  public void setSortFields(List<ISortField> sortFields) {
    this.sortFields = sortFields;
  }
  
  @Override
  public String toString() {
    return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  }
  
}
